package com.hulman.oms.web;

import com.hulman.oms.bean.Result;
import com.hulman.oms.service.VehicleReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

@RestController
public class VehicleReportController
{
    @Value("${file-home}")
    private String fileHome;
    @Autowired
    private VehicleReportService vehicleReportService;

    @GetMapping("/vehicle-reports/export")
    public Result exportVehicleReports(String vehicleName, String driverName, String startDateRange) throws IOException
    {
        String fileName = "tmp_" + UUID.randomUUID() + ".xlsx";
        Path filePath = Paths.get(fileHome, fileName);
        OutputStream outputStream = Files.newOutputStream(filePath);
        vehicleReportService.exportVehicleReports(vehicleName, driverName, startDateRange, outputStream);
        return new Result(fileName);
    }
}
